package com.example.mathtest;

import com.example.mathtest.Operate;
import com.example.mathtest.R;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{

	Button btnSubmit;
	Operate test;
	TextView firstOperand, operator, secondOperand, correctAnswer;
	EditText result;
	int count;
	String[] operators = new String[] {"+","-","*","/"};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		count = 0;
		test = new Operate();
	    test.generateOperand();
	    
	    operator = (TextView)findViewById(R.id.lblOperator);
		operator.setText(operators[test.getOperator()]+"");
		Log.d("operator: ", operators[test.getOperator()]+"");
		
		firstOperand = (TextView)findViewById(R.id.lblFirstNumber);
		firstOperand.setText(test.getFirstNumber()+"");
		
		secondOperand = (TextView)findViewById(R.id.lblSecondNumber);
		secondOperand.setText(test.getSecondNumber()+"");
		
		result = (EditText)findViewById(R.id.txtResult);
		correctAnswer = (TextView)findViewById(R.id.lblCorrectAnswer);
		btnSubmit = (Button)findViewById(R.id.btnSubmit);
		btnSubmit.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		int kq;
		if (result.getText().toString().equals(""))
			kq=0;
		else
			kq = Integer.parseInt(result.getText().toString());
		
		if (test.checkAnswer(kq)) {
			count++;
			correctAnswer.setText("Correct Answer: " + count);
		}
		//generate next operator
		test.generateOperator();
		operator.setText(operators[test.getOperator()]+"");
		//generate next two operands
		test.generateOperand();
		firstOperand.setText(test.getFirstNumber() + "");
		secondOperand.setText(test.getSecondNumber() + "");
		//reset result
		result.setText(R.string.clear);
	}
}
